#!/usr/bin/env python
#-*- coding:utf-8 -*-

import pygtk
pygtk.require('2.0')
import gtk
import copy
import logging
import threading
import gobject
from time import sleep
from core import Core
from statusbar import *

log=logging.getLogger('treeview')

netlist=[]
class View():
    def __init__(self):
        self.treestore=gtk.TreeStore(
                                     gobject.TYPE_INT,
                                     gobject.TYPE_STRING,
                                     gobject.TYPE_BOOLEAN
                                     )
    def up_prs(self,blocks,block_size,totle_size):
        if blocks==-1:
            statusbar.push(0,u'正在下载'+totle_size)
            statusbar.textbox.set_text(block_size)
        elif blocks==-2:
            statusbar.push(0,u"完成")
        else:
            percentage=float(blocks)/(totle_size/block_size+1)
            statusbar.progress.set_fraction(percentage)
    def update(self,songlist):
        self.treestore.clear()
        for i in range(len(songlist)):
            self.treestore.append(None,(i+1,songlist[i].decode('gb18030','replace'),False))
    def get_model(self):
        return self.treestore
    def select_all(self):
        model=self.treestore
        for i in range(model):
            if not model[i][2]:
                Core.append_list(model[i][2], model[i][0]-1)
                model[i][2] =not model[i][2]
class DisplayModel:
    def make_view(self,model):
        self.treeview=gtk.TreeView(model)
        self.renderer=gtk.CellRendererText()
        self.renderer.set_property('editable',False)
        self.renderer1=gtk.CellRendererText()
        self.renderer1.set_property('editable',False)
        self.renderer2=gtk.CellRendererToggle()
        self.renderer2.set_property('activatable',True)
        self.renderer2.connect('toggled',self.coll_toggled_cb,model)
        self.column=gtk.TreeViewColumn(u"编号",self.renderer,text=0)
        self.column1=gtk.TreeViewColumn(u"歌曲名字",self.renderer1)
        self.column1.add_attribute(self.renderer1,"text",1)
        self.column2=gtk.TreeViewColumn(u"选中",self.renderer2)
        self.column2.add_attribute(self.renderer2,"active",2)
        self.treeview.append_column(self.column)
        self.treeview.append_column(self.column1)
        self.treeview.append_column(self.column2)
        return self.treeview
    def coll_toggled_cb(self,cell,path,model):
        model[path][2]=not model[path][2]
        if model[path][2]:
            Core.append_list(model[path][1],model[path][0]-1)
        else:
            try:
                Core.get_netlist().remove(model[path][0]-1)
                Core.get_songlist().remove(model[path][1])
            except:
                print "不存在或已经下载"
                pass
        print Core.get_songlist()
    def select_all(self):
        model=self.treeview.get_model()
        for i in range(len(model)):
            if not model[i][2]:
                Core.append_list(model[i][1], model[i][0]-1)
                model[i][2] =not model[i][2]
    def remove_all(self):
        model=self.treeview.get_model()
        for i in range(len(model)):
            if model[i][2]:
                try:
                    Core.get_netlist().remove(model[i][0]-1)
                    Core.get_songlist().remove(model[i][1])
                except:
                    print "本来就没有这个东西"
                    pass
                model[i][2]=not model[i][2]
    def up_prs(self, blocks, block_size, total_size):
        if blocks == -1:
            statusbar.push(0,u'正在下载'+total_size+'...')
            statusbar.textbox.set_text(block_size)
        elif blocks == -2:
            statusbar.push(0,u'已完成.')
        else:
            percentage = float(blocks) / (total_size/block_size+1)
            statusbar.progress.set_fraction(percentage)
        print Core.get_netlist()
            
        
        
